Angular / Steps / Step 3: Routing
Routing
-
Step
Summary
1. routes and child routes
In the above picture, we can see some menu links 'Dashboard','Orders', 'Products' etc... in the left panel. These links are called main routes
In the picture , we can see a tab 'All products','Active Products','Under Review', 'Sold Out' etc in the right panel. These links are called child routes of the main route 'Products'
2. Create main route
1. componentsWe have 5 main routes. So we need to create 5 components
2. import the new components in the main app routesng g c pages/dashboard ng g c pages/orders ng g c pages/products ng g c pages/transactions ng g c pages/settings src/app/app.routes.ts
3. add links in the main html pageimport { Routes } from '@angular/router'; import { DashboardComponent } from './pages/dashboard/dashboard.component'; import { OrdersComponent } from './pages/orders/orders.component'; import { ProductsComponent } from './pages/products/products.component'; import { TransactionsComponent } from './pages/transactions/transactions.component'; import { SettingsComponent } from './pages/settings/settings.component'; export const routes: Routes = [ { path: '', component: DashboardComponent, }, { path: 'orders', component: OrdersComponent }, { path: 'products', component: ProductsComponent }, { path: 'transactions', component: TransactionsComponent }, { path: 'settings', component: SettingsComponent } ]; src/app/app.component.html
4. main component{{ title }}
Routes Result
src/app.component.ts
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'angular-routing'; } 3. Child route
1. child components
2. import componentsng g c pages/products/allProducts --module=app ng g c pages/products/activeProducts --module=app ng g c pages/products/reviewProducts --module=app ng g c pages/products/soldProducts --module=app src/app/app.routes.ts
import { AllProductsComponent } from './pages/products/allProducts/allProducts.component'; import { ActiveProductsComponent } from './pages/products/activeProducts/activeProducts.component'; import { ReviewProductsComponent } from './pages/products/reviewProducts/reviewProducts.component'; import { SoldProductsComponent } from './pages/products/soldProducts/soldProducts.component'; export const routes: Routes = [ { path: '', component: DashboardComponent, }, { path: 'orders', component: OrdersComponent }, { path: 'products', component: ProductsComponent, children: [ { path: '', component: AllProductsComponent }, { path: 'active', component: ActiveProductsComponent }, { path: 'review', component: ReviewProductsComponent }, { path: 'sold', component: SoldProductsComponent }, ], }, { path: '', children: [ { path: 'admin-login', component: AllProductsComponent }, { path: 'admin-dashboard', component: ActiveProductsComponent } ], }, { path: 'transactions', component: TransactionsComponent }, { path: 'settings', component: SettingsComponent } ]; Note children routes: with and without empty path 3. add child linksproducts.component.html